package com.flying.kafka.interceptor;

import com.flying.kafka.Cat;
import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.header.Headers;

import java.util.Map;
import java.util.UUID;

public class DataProducerMetricsInterceptor implements ProducerInterceptor<String, Cat> {

    @Override
    public ProducerRecord<String,Cat> onSend(ProducerRecord<String,Cat> record) {
        Headers headers = record.headers();
        String traceId = UUID.randomUUID().toString();
        headers.add("traceId", traceId.getBytes());
        headers.add("id", traceId.getBytes());
        System.out.println("Created traceId: " + traceId);
        return record;
    }

    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        if (exception != null) {
            System.out.println("producer send exception:" + exception.getMessage());
        } else {
            System.out.println(String.format("acked topic=%s, partition=%d, offset=%d",
                    metadata.topic(), metadata.partition(), metadata.offset()));
        }
    }

    @Override
    public void close() {
    }

    @Override
    public void configure(Map<String, ?> configs) {
    }
}
